import { CmdCode } from '../code/index';
import { CmdSignModel, CustomerModel } from '../models';
import { AHttpCmd } from './base.cmd';
import { cacheService } from '../services';
import Taro from '@tarojs/taro';
import { saveToModel } from '../../utils/util';
import { MODEL_person_Info, MODEL_UserId, MODEL_Pwd } from '../../utils/constants';

export class SessionPostCmd extends AHttpCmd {
  constructor(private model: CustomerModel) {
    super();
  }

  req(): CmdSignModel {
    let cmdSign = super.buildCmdSign(this.model);
    return cmdSign;
  }

  resp(model: CmdSignModel): void {
    let customer: CustomerModel = model.source;
    let modeInfo: any = {
      headImg: customer.avatarUrl
    }
    saveToModel(MODEL_person_Info, modeInfo);
    saveToModel(MODEL_UserId, customer.imUsername);
    saveToModel(MODEL_Pwd, customer.imPassword);
    cacheService.setSession(customer);
    cacheService.setSessionToken(model.sessionToken);
    Taro.eventCenter.trigger("userInit", customer);
  }

  getCmdCode(): string {
    return CmdCode.STORE_SESSION_POST;
  }

  getCmdVersion() {
    return '1'
  }
}
